#include <iostream>
#include <thread>
#include <functional>
#include <chrono>

class IntReader {
public:
    void beginRead(const std::function<void(int)>& callback){
        std::thread thread([callback](){
            std::this_thread::sleep_for(std::chrono::seconds(2));//模拟耗时操作
            int value = 1;
            callback(value);
        });
        thread.detach();
    }
};

void printInt(){
    IntReader reader1;
    int total = 0;

    reader1.beginRead([&total](int result1){
        total = result1; //第一次赋值

        IntReader reader2;
        reader2.beginRead([&total](int result2){
            total += result2; //累加第二次结果

            IntReader reader3;
            reader3.beginRead([&total](int result3){
                total += result3; //累加第三次结果
                std::cout << total << std::endl;
            });
        });
    });
}

int main(){
    printInt();
    while(1){};
}